<?php
/**
 * @title 617. 合并二叉树 - 递归
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param TreeNode $t1
     * @param TreeNode $t2
     * @return TreeNode
     */
    function mergeTrees($t1, $t2) {
        /**
         * @since 2020-09-23 某个树为空, 返回另外一个树
         */
        if($t1 === null){
            return $t2;
        } elseif($t2 === null) {
            return $t1;
        } else {
            /**
             * @since 2020-09-23 合并2个树根节点
             */
            // $root = new TreeNode($t1->val + $t2->val);
            $t1->val += $t2->val;

            /**
             * @since 2020-09-23 合并左右子树
             */
            // $root->left = self::mergeTrees($t1->left, $t2->left);
            // $root->right = self::mergeTrees($t1->right, $t2->right);
            $t1->left = self::mergeTrees($t1->left, $t2->left);
            $t1->right = self::mergeTrees($t1->right, $t2->right);

            /**
             * @return
             */
            // return $root;
            return $t1;
        }
    }
}

/**
 * @url http://127.0.0.1/leetcode/202009/2020.09.23.php
 */
$datas = [
    [
        [1,3,2,5],
        [2,1,3,null,4,null,7],
    ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    // var_dump($data);

    $obj = new Solution();
    // $result = $obj->(...$data);
    $result = $obj->mergeTrees($xhprof->tree($data[0]), $xhprof->tree($data[1]));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
